module.exports = function(grunt) {
  // ***********************live reload***************************** //
  var lrPort = 35729; // 这个是默认端口
  // 自动生成一个 livereload script标签
  var lrSnippet = require('connect-livereload')({ port: lrPort });
  var lrMiddleware = function(connect, options) {
    return [
      // 把脚本注入到静态页面
      lrSnippet,
      // 静态服务器的路径
      connect.static(options.base[0]),
      // 启动目录浏览
      connect.directory(options.base[0])
    ]
  };

  // gruntjs 配置
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    // 监听jade和less模板，自动生成调试页面
    watch: {
      configFiles: {
        files: ['jade.json', 'Gruntfile.js'],
        options: {
          reload: true,
        },
        
        tasks: ['jade', 'prettify', 'less']
      },
      
      jade: {
        files: ['jade/**/*'],
        tasks: ['jade', 'prettify'],
      },
      
      less: {
        files: ['less/**/*'],
        tasks: ['less'],
      },

      // 检查js语法 、压缩
      jshint: {
        files: ['static/js/**/*'],
        tasks: ['jshint', 'uglify'],
      },

      // css检查 、压缩
      csslint: {
        files: ['static/pages/**/*'],
        tasks: ['csslint', 'cssmin:minify'],
      },

      // livereload
      client: {
        options: {
          livereload: lrPort
        },

        files: ['html/**/*.html', 'static/pages/**/*.css', 'static/js/**/*', 'static/images/**/*']
      },
    },
    
    // jade, 把jade模板转换成html页面
    jade: {
      dev: {
        options: {
          pretty: true,
          client: false, // 编译成js模板
          // 模板中可以使用的变量
          data: grunt.file.readJSON("jade.json"),
        },
        
        files: [
          {
            expand: true,
            cwd: './jade',
            src: '**/*.jade',
            dest: 'html/',
            ext: '.html',
            extDot: 'first',
          },
        ],
      },
    },
    
    prettify: {
      compile: {
        options: {
          indent: 2,
          condense: true, // 移除空行，保持缩进
          indent_inner_html: true,
          unformatted: [
            // html tag name
          ],

          padcomments: true, // 注释前加空行
        },

        files: [
          {
            expand: true,
            flatten: false,
            cwd: './html',
            src: '**/*.html',
            dest: './html',
            ext: '.html',
            extDot: 'first',
          },
        ],
      }
    },
    
    less: {
      compile: {
        options: {
          compress: true,
        },
        
        files: [
          {
            expand: true,
            cwd: './less',
            src: '**/*.less',
            dest: './static/pages',
            ext: '.css',
            extdot: 'first',
          },
        ],
      },
    },

    jshint: {
      options: {
        eqeqeq: true, // 强制使用===
        trailing: true // 行尾无空格
      },

      files: ['static/js/**/*.js', '!**/*.min.js'],
    },

    uglify: {
      common: {
        options: {
          compress: {
            drop_console: false,
          }
        },

        files: [
          {
            expand: true,
            cwd: 'static/js',
            src: ['**/*.js', '!*.min.js'],
            dest: 'static/js',
            ext: '.min.js',
            extDot: 'first',
          },
        ],
      },
    },

    csslint: {
      options: {
        import: false,
      },

      src: ['static/pages/**/*.css', '!**/*.min.css'],
    },

    cssmin: {
      minify: {
        expand: true,
        cwd: 'static/pages/',
        src: ['**/*.css', '!**/*.min.css'],
        dest: 'static/pages/',
        ext: '.min.css'
      },

      combine: {
        files: {
          'css/out.min.css': ['css/part1.min.css', 'css/part2.min.css']
        }
      }
    },

    imagemin: {
      minify: {
        options: {
          optimizationLevel: 3, // 定义优化水平 -- png
        },

        files: [
          {
            expand: true,
            cwd: 'static/images',
            src: ['**/*.{png,jpg,jpeg}'],
            dest: 'static/images', // 直接覆盖
          }
        ],
      },
    },

    // live reload
    connect: {
      options: {
        port: process.env.PORT, // 服务器端口
        hostname: process.env.IP,
        // 物理路径(默认为. 即根目录) 注：使用'.'或'..'为路径的时，可能会返回403 Forbidden. 此时将该值改为相对路径 如：/grunt/reloard。
        base: '.'
      },

      livereload: {
        options: {
          // 通过LiveReload脚本，让页面重新加载。
          middleware: lrMiddleware
        },
      },
    },

    // 清除自动编译的文件
    clean: {
      jade: ['html/**/*.html'],
      uglify: ['static/js/**/*.min.js'],
      less: ['static/pages/**/*.css'],
      cssmin: ['static/pages/**/*min.css'],
    },
  });
  
  // 加载gruntjs插件
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-jade');
  grunt.loadNpmTasks('grunt-prettify');
  grunt.loadNpmTasks('grunt-contrib-less');
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-csslint');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-contrib-imagemin'); // 图片压缩
  // 实时调试
  grunt.loadNpmTasks('grunt-contrib-connect');

  // 配置任务
  grunt.registerTask('default', ['connect', 'watch']);
  // 图片优化 PNG, JPEG, GIF and SVG
  grunt.registerTask('optimize', ['cssmin', 'uglify', 'imagemin']);
  // build
  grunt.registerTask('build', ['jade', 'prettify', 'less']);
}